package com.soo.hoo.admin.api;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.soo.hoo.admin.model.entity.SysSearchRecord;
import com.soo.hoo.admin.service.SearchRecordService;
import com.soo.hoo.core.annotation.OperateLog;
import com.soo.hoo.core.enums.BusinessTypeEnum;
import com.soo.hoo.core.model.BaseEntry;
import com.soo.hoo.core.model.Page;
import com.soo.hoo.core.model.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 搜索记录
 * </p>
 *
 * @author 杨燚
 * @since 2024/5/7
 */
@Tag(name = "搜索记录")
@RequestMapping("/search/")
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SearchRecordController {


    private final SearchRecordService searchRecordService;

    @PostMapping("create")
    @Operation(summary = "保存搜索记录")
    @OperateLog(title = "保存搜索记录", businessType = BusinessTypeEnum.CREATE)
    public Result<Void> create(@RequestBody Map<String, Object> record) {
        Object searchValue = record.get("searchValue");
        if (searchValue != null && StrUtil.isNotBlank(searchValue.toString())) {
            SysSearchRecord searchRecord = new SysSearchRecord();
            searchRecord.setRecordContent(searchValue.toString());
            searchRecord.setRecordTime(LocalDateTime.now());
            searchRecordService.save(searchRecord);
        }
        return Result.success();
    }

    @GetMapping("list")
    @Operation(summary = "搜索记录列表")
    @OperateLog(title = "搜索记录列表", businessType = BusinessTypeEnum.QUERY)
    public Result<List<String>> list() {
        Page<SysSearchRecord> pagination = new Page<>(1L, 10L, null, null, null);
        OrderItem createAt = new OrderItem("createAt", false);
        List<OrderItem> orderItems = ListUtil.of(createAt);
        if (CollUtil.isEmpty(pagination.getOrderItems())) {
            pagination.setOrderItems(orderItems);
        } else {
            pagination.getOrderItems().addAll(pagination.getOrderItems());
        }
        Page<SysSearchRecord> page = searchRecordService.page(pagination);
        List<String> list = new ArrayList<>();
        if (CollUtil.isNotEmpty(page.getRecords())) {
            list = page.getRecords()
                    .stream().map(SysSearchRecord::getRecordContent).toList();
        }
        return Result.success(list);
    }

    @DeleteMapping("clear")
    @Operation(summary = "清空搜索记录")
    @OperateLog(title = "清空搜索记录", businessType = BusinessTypeEnum.DELETE)
    public Result<Void> clear() {
        Wrapper<SysSearchRecord> eq = new LambdaQueryWrapper<SysSearchRecord>()
                .eq(BaseEntry::getCreateBy, StpUtil.getLoginIdAsLong());
        List<SysSearchRecord> list = searchRecordService.list(eq);
        searchRecordService.removeByIds(
                list.stream().map(SysSearchRecord::getId).toList()
        );
        return Result.success();
    }
}
